package ru.dadle8.service.impl;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.dadle8.model.bot.entity.MessageCallbackData;
import ru.dadle8.service.api.MessageService;
import ru.dadle8.service.api.bot.ContentMessageService;
import ru.dadle8.service.api.bot.KeyboardService;
import ru.dadle8.service.api.bot.MenuActionService;
import ru.dadle8.service.api.bot.Storage;
import ru.dadle8.service.api.db.UserService;
import ru.dadle8.service.api.db.WorkoutService;
import ru.dadle8.service.api.helper.MessageHelper;

@Getter
public abstract class AbstractMessageService implements MessageService {
    @Autowired
    private TelegramLongPollingBot bot;
    @Autowired
    private ContentMessageService contentMessageService;
    @Autowired
    private KeyboardService keyboardService;
    @Autowired
    private Storage storage;
    @Autowired
    private MenuActionService menuActionService;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserService userService;
    @Autowired
    private WorkoutService workoutService;

    private Long chatId;
    private User user;
    private Message inMess;
    private CallbackQuery callbackQuery;
    private MessageCallbackData callbackData;
    private Integer messageId;

    @Override
    public void execute(Update update) throws TelegramApiException {
        init(update);
        preAction(update);
        processUpdate(update);
    }

    protected void init(Update update) {
        if (update.hasMessage()) {
            chatId = update.getMessage().getChatId();
            user = update.getMessage().getFrom();
            inMess = update.getMessage();
            messageId = update.getMessage().getMessageId();
        } else if (update.hasCallbackQuery()) {
            callbackQuery = update.getCallbackQuery();
            callbackData = getMessageHelper().getMessageCallbackData(callbackQuery);
            chatId = callbackQuery.getMessage().getChatId();
            messageId = callbackQuery.getMessage().getMessageId();
        }
    }

    protected void preAction(Update update) throws TelegramApiException {

    }

    public abstract void processUpdate(Update update) throws TelegramApiException;
}
